# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os,base64
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import Img
import datetime
# Create your views here.

def welcome(request):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    files = request.FILES.getlist('myFile')
    ss = ""
    for f in files:
        destination = open(BASE_DIR + '\\media\\IMG\\a2\\' + f.name, 'wb+')  # 上传的文件都放到/media/photos/文件夹里
        ss = ss + ',' + f.name

        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()

    ss1 = ss[1:]
    print ss1
    img = Img(img_url=ss1)
    img.save()
    return render(request, 'show.html')
def welcome1(request):
    if request.user.is_authenticated:
        ooo = request.session.get(u'_auth_user_id', None)
        #aa = User.objects.get(id=ooo)
        #ss = aa.username
        oo1 = request.GET.get("name", None)
        ll1 = request.GET.get("uid", None)
        context = {'name': ooo, 'll1': ll1, 'oo1': oo1}
        if request.method == "POST":
            username = request.POST.get('username')
            dj = request.POST.get("dj")
            mobile = request.POST.get('mobile')
            bz = request.POST.get('bz')
            dic = {"vid": ll1, "fname": oo1, "username": username, "dj": dj, 'mobile': mobile, 'bz': bz}
            vr.objects.create(**dic)
            return HttpResponseRedirect(u'/admin/app/dd/app/user1/{0}/change/'.format(ooo))


    else:
        return HttpResponseRedirect('/admin/app/dd/login/')

    return render(request, 'admin/app/vr/add.html', context)
   # return render(request, 'admin/app/vr/add.html')

def vr(request):
    return render(request, 'admin/app/vr/add.html')

'''
def uploadImg(request):
    if request.method == 'POST':
        img = Img(img_url=request.FILES.get('img'))
        img.save()
    return render(request, 'imgUpload.html')
'''
def uploadImg(request):
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    files = request.FILES.getlist('img')
    ss = ""
    for f in files:
        destination = open(BASE_DIR + '/media/IMG/' + f.name,'wb+')      #上传的文件都放到/media/photos/文件夹里
        ss=ss+','+f.name

        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()

    ss1=ss[1:]
    print ss1
    img = Img(img_url=ss1)
    img.save()

    return render(request, 'imgUpload.html')
def data1(request):
    if request.method == 'POST':
        fomrmat = request.POST.get("imgFormat")
        user1 = request.POST.get("imgBase")

        u2 = request.POST.get("lookIndex")
        now = datetime.datetime.now()
        time_str3 = now.strftime('%Y%m%d%H%M%S')
        file_address = time_str3+u2+ r"."+fomrmat
        all1=user1.split(',')

        ss1=all1[1]
        imgdata = base64.b64decode(ss1)
        file = open(file_address, 'wb')
        file.write(imgdata)
        file.close()
        return HttpResponse("上传成功")
    else:
         print 'abc'
         return HttpResponse('操作失败')
